#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/2/28 22:10
import common
from searcher import Searcher
import json

import requests

from flask import current_app


class BaiduSearcher(Searcher):
    def search(self):
        url = f'https://www.baidu.com/s?wd={self.keyword}&pn=1&rn={self.num}&tn=json'
        cookie = self.cookie if self.cookie else current_app.config.get('DEFAULT_BAIDU_COOKIE')

        headers = {
            'User-Agent': f'{self.user_agent}',
            'Cookie': f'{cookie}'
        }
        response = requests.get(url, headers=headers)
        common.get_logger().info(
            f'百度搜索：{self.keyword}，状态码：{response.status_code}，返回结果长度：{len(response.content) / 1024}K')
        return json.dumps(response.json(), ensure_ascii=False)
